﻿using eLoan.Models;
using System;
using System.Linq;
using System.Web.Mvc;

namespace eLoan.Controllers
{
    public class LoanCoMakerController : Controller
    {
        public ActionResult AddEdit(int? id, string documentNo)
        {
            try
            {
                var db = new DatabaseContext();
                var loan = db.Loans.Where(w => w.DocumentNo == documentNo).SingleOrDefault<Loan>();

                var model = new LoanCoMakerModel();
                model.BorrowerID = loan.BorrowerID;
                model.BorrowerName = AdministrationHelper.GetBorrowerName(loan.BorrowerID);
                model.BranchCode = loan.BranchCode;
                model.DocumentNo = documentNo;

                if (id == null)
                {
                    model.DBCommand = AdministrationHelper.DBCommand.Add.ToString();
                    model.LoanCoMaker = new LoanCoMaker();
                    model.LoanCoMaker.DocumentNo = documentNo;
                    model.LoanCoMaker.CoMakerName = string.Empty;
                    model.LoanCoMaker.Address = string.Empty;
                    model.LoanCoMaker.ContactNumbers = string.Empty;
                    model.LoanCoMaker.IDNo = string.Empty;

                    return View(model);
                }
                else
                {
                    model.DBCommand = AdministrationHelper.DBCommand.Edit.ToString();
                    model.LoanCoMaker = db.LoanCoMakers.Where(w => w.ID == id).SingleOrDefault<LoanCoMaker>();

                    return View(model);
                }
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        public ActionResult Delete(int? id, string documentNo)
        {
            try
            {
                var model = new LoanCoMakerModel();
                var db = new DatabaseContext();
                var loan = db.Loans.Where(w => w.DocumentNo == documentNo).SingleOrDefault<Loan>();

                model.DocumentNo = documentNo;
                model.BorrowerID = loan.BorrowerID;
                model.BorrowerName = AdministrationHelper.GetBorrowerName(loan.BorrowerID);
                model.BranchCode = loan.BranchCode;
                model.LoanCoMaker = db.LoanCoMakers.Where(w => w.ID == id).SingleOrDefault<LoanCoMaker>();

                return View(model);
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        [HttpPost]
        public ActionResult DeleteLoanCoMaker(int? id, string documentNo, int? borrowerID, string branchCode, string command)
        {
            try
            {

                if (command == AdministrationHelper.DBCommand.Delete.ToString())
                {
                    using (var db = new DatabaseContext())
                    {
                        var coMaker = db.LoanCoMakers.Where(w => w.ID == id).SingleOrDefault<LoanCoMaker>();
                        db.LoanCoMakers.Remove(coMaker);
                        db.SaveChanges();
                    }
                }
                else
                {
                    // cancelled
                }

                return RedirectToAction("LoanCoMakersAndCollaterals", "Loan", new { documentNo = documentNo, borrowerID = borrowerID, branchCode = branchCode });
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        [HttpPost]
        public ActionResult Save(LoanCoMakerModel model, string afterSave)
        {
            try
            {
                string documentNo = "0";
                var arr = afterSave.Split(',');
                documentNo = arr[1];

                using (var db = new DatabaseContext())
                {
                    if (arr[0] == AdministrationHelper.DBCommand.Add.ToString())
                    {
                        db.LoanCoMakers.Add(model.LoanCoMaker);
                        db.SaveChanges();
                    }
                    else if (arr[0] == AdministrationHelper.DBCommand.Edit.ToString())
                    {
                        db.Entry(model.LoanCoMaker).State = System.Data.EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        // cancelled
                    }
                }

                return RedirectToAction("LoanCoMakersAndCollaterals", "Loan", 
                    new { documentNo = documentNo, 
                            borrowerID = model.BorrowerID, 
                            branchCode = model.BranchCode });
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }
    }
}
